home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 007 / progchk.c < prev    next >
Text File  |  1985-06-03  |  4KB  |  170 lines

  1. /*      Program   PROGCHK.C      */
  2.  
  3. /*   This program scans the "C" source file for possible   */
  4. /*   syntax errors.  Even though the results may not always*/
  5. /*   be accurate, it could isolate a hard-to-find bug.     */
  6. /*          This routine checks for:                       */
  7. /*            1.  Unmatched braces.                        */
  8. /*            2.  Unmatched parentheses.                   */
  9. /*            3.  Unmatched single and double quotes.      */
  10. /*            4.  Nested comments.                         */
  11.  
  12. /*   This routine also provides comment and total line counts.  */
  13.  
  14.  
  15. #define NULL 0
  16. #define TRUE 1
  17. #define FALSE 0
  18. #define EOF (-1)
  19. #define SEMI 59
  20. #define TAB 9
  21.  
  22. typedef int FILE;
  23. char stra[100];
  24. int lincnt, erflag, linptr;
  25. char linbuf[256];
  26.  
  27. main(argc,argv)
  28. int argc;
  29. char *argv[];
  30. {
  31.     int c=0, i=0, lefpar=0, rigpar=0;
  32.     int dquote=0,lefbra=0,rigbra=0;
  33.     int comcnt=0,incom=0,comyes=0,squote=0;
  34.     int prevchr=0, nestyes=0;
  35.     FILE *in_file;
  36.  
  37.     if (argc < 2)
  38.         {
  39.         printf (" This program checks for possible syntax errors.\n");
  40.         printf (" To invoke it's use:  E>progchk filename.c \n\n");
  41.         exit(1);
  42.         }
  43.     if ((in_file = fopen(argv[1],"r")) ==  NULL)
  44.         {
  45.         printf (" Cannot open %s for input.\n",argv[1]);
  46.         exit(1);
  47.         }
  48.  
  49.     lincnt = 0;               /*  Setup variables */
  50.     linptr = 0;
  51.     erflag = FALSE;
  52.     for (i=0; i<255; ++i )
  53.         linbuf[i] = NULL;
  54.  
  55.     while (( c = fgetc(in_file)) != EOF )   /*  Main loop */
  56.     {
  57.  
  58.     switch (c)
  59.     {
  60.         case '{':
  61.             lefbra++;
  62.             break;
  63.         case '}':
  64.             rigbra++;
  65.             break;
  66.         case '(':
  67.             lefpar++;
  68.             break;
  69.         case ')':
  70.             rigpar++;
  71.             break;
  72.         case '/':
  73.             if (prevchr == '*')
  74.                 {
  75.                 if (incom == FALSE)
  76.                     nestyes = TRUE;
  77.                 incom = FALSE;
  78.                 comyes = TRUE;
  79.                 }
  80.             break;
  81.         case '*':
  82.             if (prevchr == '/')
  83.                 {
  84.                 if (incom == TRUE)
  85.                     nestyes = TRUE;
  86.                 incom = TRUE;
  87.                 comyes = TRUE;
  88.                 }
  89.             break;
  90.         case '\'':
  91.             if (squote == TRUE )
  92.                 squote = FALSE;
  93.             else
  94.                 squote = TRUE;
  95.             break;
  96.         case '\"':
  97.             if (dquote == TRUE )
  98.                 dquote = FALSE;
  99.             else
  100.                 dquote = TRUE;
  101.             break;
  102.         case '\n' :
  103.             lincnt++;
  104.             if (squote == TRUE)
  105.                 {
  106.                 lerr("possible unbalanced single quotes.");
  107.                 squote = FALSE;
  108.                 }
  109.             if (dquote == TRUE)
  110.                 {
  111.                 lerr("possible unbalanced double quotes.");
  112.                 dquote = FALSE;
  113.                 }
  114.             if (incom == TRUE || nestyes == TRUE)
  115.                 {
  116.                 lerr("possible nested comments.");
  117.                 incom = FALSE;
  118.                 nestyes = FALSE;
  119.                 }
  120.             if (comyes == TRUE)
  121.                 {
  122.                 comcnt++;
  123.                 comyes = FALSE;
  124.                 }
  125.             if (lefpar != rigpar)
  126.                 {
  127.                 lerr("possible unbalanced parens.");
  128.                 lefpar = 0;
  129.                 rigpar = 0;
  130.                 }
  131.             for (i=0; i < 255; ++i )
  132.                 linbuf[i] = NULL;
  133.             erflag = FALSE;
  134.             linptr = 0;
  135.             break;
  136.     }
  137.     prevchr = c;
  138.     if ((i = isprint(c)) == TRUE || c == TAB)
  139.         linbuf[linptr] = c;
  140.     linptr++;
  141.     }
  142.  
  143.     printf ("\n\n");
  144.     if(lefbra > rigbra)
  145.         printf (" --- File has unbalanced braces---too many {. \n");
  146.     if (rigbra > lefbra)
  147.         printf (" --- File has unbalances braces---too many }. \n");
  148.  
  149.     printf ("\n\n  File: %s contains %d comment lines and %d total lines.\n",argv[1],comcnt,lincnt);
  150.     fclose (in_file);
  151. }
  152.  
  153. /*   Error function. */
  154.  
  155. lerr(stra)
  156. char stra;
  157. {
  158.     int i;
  159.     if (erflag == FALSE)
  160.         {
  161.         printf ("Line: %d >>>> ",lincnt);
  162.         for (i=0; linbuf[i] == NULL || i < 255; ++i)
  163.             printf ("%c",linbuf[i]);
  164.         printf ("\n");
  165.         erflag = TRUE;
  166.         }
  167.     printf ("     --- %s \n",stra);
  168. }
  169. L || i < 255; ++i)
  170.             printf ("%c",linbuf[i])